package com.wmd102.opengldemo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.ref.WeakReference;
import java.lang.reflect.Method;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.ByteBuffer;
import java.text.SimpleDateFormat;
import java.util.Date;

import static com.wmd102.opengldemo.DMGLSurfaceView.IMAGE_FORMAT_RGBA;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getSimpleName();

    private static final int SAMPLE_TYPE  =  200;

    public static final int SAMPLE_TYPE_KEY_TRIANGLE = 0;//三角形
    public static final int SAMPLE_TYPE_KEY_SQUARE = 1;//正方形/長方形
    public static final int SAMPLE_TYPE_KEY_CUBE = 2;//正方體
    public static final int SAMPLE_TYPE_KEY_CIRCULAR = 3;//圓形
    public static final int SAMPLE_TYPE_KEY_CONE = 4;//圆锥
    public static final int SAMPLE_TYPE_KEY_CYLINDER = 5;//圆柱
    public static final int SAMPLE_TYPE_KEY_SPHERE = 6;//球体
    public static final int SAMPLE_TYPE_KEY_LIGHTSPHERE = 7;//带光源球体
    public static final int SAMPLE_TYPE_KEY_TEXTURE = 8;//纹理贴图
    public static final int SAMPLE_TYPE_KEY_TEXTURE_SPERE = 9;//球体纹理贴图

    private String[] mSampleArray = new String[]{
            "三角形",
            "正方形",
            "正方体",
            "圆形",
            "圆锥",
            "圆柱",
            "球体",
            "带光源球体",
            "纹理贴图",
            "球体纹理贴图",     };

    private int mSampleCurrentIndex = 0;

    private NativeInterface mNativeInterface;
    private DMGLSurfaceView mDMGLSurfaceView;
    private TextView mSampleText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mNativeInterface = NativeInterface.getInstance();

        setContentView(R.layout.activity_main);
        mDMGLSurfaceView = (DMGLSurfaceView)findViewById(R.id.dm_gl_surface_view);
        mSampleText = (TextView)findViewById(R.id.sample_text);

        // Example of a call to a native method
//        TextView tv = findViewById(R.id.sample_text);
//        tv.setText(mNativeInterface.native_GetLibVersion());
//        mNativeInterface = null;
        mNativeInterface.native_OnInit();
    }

    @Override
    protected void onResume() {
        super.onResume();
//        mNativeInterface.native_OnInit();
        mSampleCurrentIndex = mSampleArray.length - 1;

        renderRun();

        mSampleText.setText(mSampleArray[mSampleCurrentIndex]);
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mNativeInterface.native_OnUnInit();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.select_sample_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.select_sample:
                showSelectSampleDialog();
                break;
            default:
                break;
        }
        return true;
    }

    private WeakReference<AlertDialog> mAlertDialog;
    private void showSelectSampleDialog() {
        AlertDialog alertDialog = new AlertDialog.Builder(this)
        .setTitle(R.string.select_sample)
        .setSingleChoiceItems(mSampleArray, mSampleCurrentIndex,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Log.d(TAG, "onClick position:" + i);
                        mSampleCurrentIndex = i;
                        renderRun();
                        mSampleText.setText(mSampleArray[mSampleCurrentIndex]);
                        AlertDialog alertDialog = mAlertDialog.get();
                        if(alertDialog != null){
                            alertDialog.dismiss();
                        }
                    }
                })
        .show();
        mAlertDialog = new WeakReference<AlertDialog>(alertDialog);
    }

    private void renderRun(){
        mNativeInterface.native_SetParamsInt(SAMPLE_TYPE, mSampleCurrentIndex);

        switch (mSampleCurrentIndex){
            case SAMPLE_TYPE_KEY_TEXTURE:
                loadRGBAImage(R.drawable.texture_image);
                break;
            case SAMPLE_TYPE_KEY_TEXTURE_SPERE:
                loadRGBAImage(R.drawable.earth_512_512);
                break;
            default:
                break;
        }

        mDMGLSurfaceView.requestRender();
    }

    private Bitmap loadRGBAImage(int resId) {
        InputStream is = this.getResources().openRawResource(resId);
        Bitmap bitmap;
        try {
            bitmap = BitmapFactory.decodeStream(is);
            if (bitmap != null) {
                int bytes = bitmap.getByteCount();
                ByteBuffer buf = ByteBuffer.allocate(bytes);
                bitmap.copyPixelsToBuffer(buf);
                byte[] byteArray = buf.array();

                Log.d(TAG, "loadRGBAImage w:" + bitmap.getWidth()+" h:"+bitmap.getHeight());

                mNativeInterface.native_SetImageData(
                        IMAGE_FORMAT_RGBA, bitmap.getWidth(), bitmap.getHeight(), byteArray);
            }
        }
        finally
        {
            try
            {
                is.close();
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
        }
        return bitmap;
    }
}